package mis.web;

import mis.domain.User;
import mis.service.UserManager;

import java.util.Date;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class UserController{

    protected final Log logger = LogFactory.getLog(getClass());

    private UserManager userManager;
    private final String LIST_ACTION = "redirect:/user/getUserList.do";
    
    @Autowired
    public void setUserManager(UserManager userManager)
    {
    	this.userManager = userManager;
    }
    
    
    /**
     * get UserList
     */
    @RequestMapping(value="/user/getUserList",method = RequestMethod.GET)
    public String getUserList(Map<String, Object> myModel)
    {
        myModel.put("users", this.userManager.getUserList());
        return "userList";
    }
 
    
    /**
     * get User
     */
    @RequestMapping(value="/user/getUser",method = RequestMethod.GET)
    public String getUser(Map<String, Object> myModel,@RequestParam int id)
    {
    	User user = userManager.getUser(id);
        myModel.put("user", user);
        logger.info("controller get user:" + user);
        return "userView";
    }
    
    /**
     * add/update user
     */
    @RequestMapping(value="/user/updateUser",method = RequestMethod.POST)
    public ModelAndView  updateUser(@ModelAttribute("user") User user,@RequestParam String action){
		//check the operation type
    	
    	logger.info("controller updateUser action: " + action + ",user:" + user);
    	if("add".equalsIgnoreCase(action))
    	{
    		user.setCreateTime(new Date());
    		userManager.addUser(user);
    	}
    	else
    	{
    		user.setUpdateTime(new Date());
    		userManager.updateUser(user);
    	}
		return new ModelAndView(LIST_ACTION);
	}
    
    
    /**
     * remove user
     */
    @RequestMapping(value="/user/removeUser",method = RequestMethod.GET)
    public ModelAndView  removeUser(@RequestParam int id){
		logger.info("controller remove user: " + id);
		userManager.deleteUser(id);
		return new ModelAndView(LIST_ACTION);
	}
    
    /**
     * get to add/update user screen
     */
    @RequestMapping(value="/user/goToUserView",method = RequestMethod.GET)
    public String addUserView(HttpServletRequest request,
    								Map<String, Object> myModel,
    								@RequestParam String action
    		){
    	//String action = request.getParameter("action");
    	String userId = request.getParameter("id");
    	User user;
    	if("add".equalsIgnoreCase(action))
    	{
    		user = new User();
    	}
    	else
    	{
    		int id = Integer.parseInt(userId);
    		user = userManager.getUser(id);
    	}
    	logger.info("controller goToUserView action: " + action + ",user:" + user);
    	myModel.put("user", user);
    	myModel.put("action", action);
		return "userUpdate";
	}
    
    /**
     * ajax call to fetch user data base on user id
     */
    @RequestMapping("/user/fetchUserData")  
    @ResponseBody  
    public Object fetchUserData(HttpServletRequest request,
    					@RequestParam int id){  
    	
    	logger.info("--userId:" + id);
    	User user = new User();
    	user.setName("Zhang Pan");
    	return user;
    } 
}